// Cloak multiple affiliate links
// Date 23:14 12/12/2016
// By Ben a.k.a DreamVB

#include <iostream>
#include <string>

using namespace std;
using std::cout;
using std::endl;

int main(int argc, char *argv[]){
	string sLink[100];
	string sPath = "";
	int NoLinks = 0;
	int i = 0;

	cout << "--- Multiple affiliate links cloaker v2.0 ---\n\n";
	//Ask how many links to cloak.
	cout << "How many affiliate links to cloak max 100 : ";
	//Get links.
	cin >> NoLinks;

	if (NoLinks < 0 | NoLinks>100){
		cout << "Invaild number of links\n";
		exit(1);
	}

	cin.ignore();

	//Fetch links from user.
	while (i < NoLinks){
		cout << "Enter Link  " << (i + 1) << " : ";
		//Get link
		std::getline(cin, sLink[i]);
		i++;
	}

	//.htaccess file
	//Ask were the .htaccess file will be saved.
	cout << endl << "Were will this php file be located eg /ads/ : ";
	std::getline(cin, sPath);

	i = 0;
	//Clear the screen
	system("cls");

	cout << "Below is the code for your index.php file.\n\n";
	cout << "--- CUT CODE ---\n\n";
	cout << "<?php\n";
	cout << "  $path = array(\n";

	while (i < NoLinks){
		//Display the link
		cout << "  'affiliatelink" << (i + 1) << "' =>  '" << sLink[i] << "',\n";
		//INC counter
		i++;
	}
	cout << ");\n";

	//Write redirect code.
	cout << "if (array_key_exists($_GET['id'], $path))\n";
	cout << "  header('Location: ' .$path[$_GET['id']]);\n";
	cout << "?>\n\n";
	cout << "--- END CODE ---\n\n";

	//Write .htaccess code.
	cout << "Below is the code for your .htaccess file.\n\n";
	cout << "--- CUT CODE ---\n\n";
	cout << "Options +FollowSymLinks -MultiViews\n";
	cout << "\nRewriteEngine On\n";
	cout << "RewriteBase " << sPath << "\n\n";
	cout << "RewriteCond %{REQUEST_FILENAME} !-d\n";
	cout << "RewriteCond %{REQUEST_FILENAME} !-f\n";
	cout << "RewriteRule ^([^/]+)$ index.php?id=$1 [L]\n";
	cout << "--- END CODE ---\n\n";
	//Clear up.
	
	system("pause");
	return EXIT_SUCCESS;
}